﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common ;
using MySql.Data.MySqlClient;

namespace DbSharp.Tools.DbConnectionBuilder.Core.Plugin
{
    class MySqlConnectionPlugin : IDbConnectionPlugin
    {

        #region IDbConnectionPlugin Members


        public PluginNamePair PluginNamePair
        {
            get { return new PluginNamePair("MySql.Data.MySqlClient") { DisplayName = "MySQL Driver" }; }
        }


        public DbConnectionStringBuilder CreateConnectionStrBuilder(DbServerCredential credential)
        {
            //server=localhost;Port=3306;user id=root;password=mysql1;database=test 
            var builder = new MySqlConnectionStringBuilder();
            builder.Database = credential.Database;
            builder.Server = credential.DataSource;
            builder.UserID = credential.UserId;
            builder.Password = credential.Password;
            builder.IgnorePrepare = true;
            uint port;
            if (uint.TryParse(credential.ServerPort, out port))
                builder.Port = port;

            return builder;
        }

        public DbServerCredential CreateCredential(string connectionString)
        {
            DbServerCredential credential = new DbServerCredential();
            credential.UserIdEnabled = true;
            credential.PasswordEnabled = true;
            credential.DataSourceEnabled = true;
            credential.DatabaseEnabled = true;
            credential.ServerPortEnabled = true;

            var builder = new MySqlConnectionStringBuilder(connectionString);
            object userId;
            if (builder.TryGetValue("User ID", out userId))
            {
                credential.UserId = userId.ToString();
            }
            object pwd;
            if (builder.TryGetValue("Password", out pwd))
            {
                credential.Password = pwd.ToString();
            }
            object dataSource;
            if (builder.TryGetValue("server", out dataSource))
            {
                credential.DataSource = dataSource.ToString();
            }
            object database;
            if (builder.TryGetValue("database", out database))
            {
                credential.Database = database.ToString();
            }
            object serverPort;
            if (builder.TryGetValue("Port", out serverPort))
            {
                credential.ServerPort = serverPort.ToString();
            }
            credential.AdditionalInfo = "";

            return credential;
        }

        public DbConnection CreateDbConnection(string connectionString)
        {
            return new MySqlConnection(connectionString);
        }

        public List<FileVersionPair> GetCandidateDbFileVersionPairs()
        {
            var list = new List<FileVersionPair>();
            return list;
        }

        #endregion
    }


}
